﻿using commonImportPlugin.Config;
using commonImportPlugin.Configs.Base;
using commonImportPlugin.Configs.Other;
using commonImportPlugin.Factorys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace commonImportPlugin.Common
{
    public class PligunUtil
    {
        GetFileUtil getFileUtil = new GetFileUtil();

        /// <summary>
        /// 初始化核心工厂处理库
        /// </summary>
        /// <param name="configCore">配置类</param>
        /// <param name="_context">上下文</param>
        /// <returns>工类集类</returns>
        public AllCores InitCores(AllFactoryNames factoryNames)
        {
            AllCores factorysCores = new AllCores();
           
            //
            //金融核心函数初始化
            //
            if (factoryNames.pbocFileFactory != null)
            {
                factorysCores.pbocFileCore = new setPbocFileCoreFactory().CreateFactory(factoryNames.pbocFileFactory);
            } 

            //
            //社保核心函数初始化
            //
            if (factoryNames.ssseFileFactory != null)
            {
                factorysCores.ssseFileCore = new setSsseFileCoreFactory().CreateFactory(factoryNames.ssseFileFactory);
            }

            //
            //磁条核心函数初始化
            //
            if(factoryNames.trackFileFactory!=null)
            {
                factorysCores.trackFileCore = new setTrackFileCoreFactory().CreateFactory(factoryNames.trackFileFactory);
            }

            //
            //医保核心函数初始化
            //
            if(factoryNames.medicalFileFactory!=null)
            {
                factorysCores.medicalFileCore = new setMedicalFileCoreFactory().CreateFactory(factoryNames.medicalFileFactory);
            }

            // 
            //匹配数据文件核心初始化
            //
            if(factoryNames.matchFileFactory!=null)
            {
                factorysCores.matchFileCore = new setMatchFileCoreFactory().CreateFactory(factoryNames.matchFileFactory);
            }

            //
            //匹配核心函数初始化
            //
            if(factoryNames.matchFactory!=null)
            {
                factorysCores.matchCore = new setMatchFactory().createFactory(factoryNames.matchFactory);
            }

            return factorysCores;
        }

        /// <summary>
        /// 获取相应文件且条数
        /// </summary>
        /// <param name="_allCores">核心工厂集</param>
        /// <param name="allDataFiles">所有制卡数据</param>
        /// <param name="dicFilterFileRules">所有筛选文件参数集</param>
        /// <param name="commentMessage">条数提示信息</param>
        /// <param name="allfiles">所有制卡数据文件类</param>
        public int InitFilesAndGetCount(AllCores _allCores,ConfigCore configCore, List<string> allDataFiles,
            Dictionary<string, string> dicFilterFileRules, out string commentMessage, out AllFiles allfiles, out Dictionary<string, int> dataCounts)
        {
            AllCores allCores = _allCores;
            int targetCounts = 0;//目标数据量
            commentMessage = "";
            allfiles = new AllFiles();
            dataCounts = new Dictionary<string, int>();

            int pbocDataCount = 0;//金融数据量
            int ssseDataCount = 0;//社保数据量
            int trackDataCount = 0;//磁条数据量
            int medicalDataCount = 0;//医保数据量
            int matchDataCount = 0;//匹配数据量

            //获取金融数据文件
            if (allCores.pbocFileCore != null)
            {
                allfiles.pbocFiles = new List<string>();
                allfiles.pbocFiles = getFileUtil.FilterFilesWithNameOrContentRegexs(allDataFiles, dicFilterFileRules["PbocFileFilterRules"], "byte");
                
                if (allfiles.pbocFiles.Count == 0)
                    throw new Exception("获取金融数据文件失败！");
                
                //获取金融数据量
                pbocDataCount = allCores.pbocFileCore.GetPbocAllCounts(allfiles.pbocFiles);
                commentMessage += string.Format("金融:{0}", pbocDataCount);
                dataCounts.Add("pboc", pbocDataCount);
            }
            //获取社保数据文件
            if (allCores.ssseFileCore != null)
            {
                allfiles.ssseFiles = new List<string>();
                allfiles.ssseFiles = getFileUtil.FilterFilesWithNameOrContentRegexs(allDataFiles, dicFilterFileRules["SsseFileFilterRules"], null);

                if (allfiles.ssseFiles.Count > 1)
                    throw new Exception("存在多份社保数据");
                if (allfiles.ssseFiles.Count == 0)
                    throw new Exception("获取社保数据文件失败！");

                ssseDataCount = allCores.ssseFileCore.GetSsseAllCounts(allfiles.ssseFiles[0]);
                commentMessage += string.Format(" 社保:{0}", ssseDataCount);
                dataCounts.Add("ssse", ssseDataCount);
            }
            //获取磁条数据文件
            if (allCores.trackFileCore != null)
            {
                allfiles.trackFiles = new List<string>();
                allfiles.trackFiles = getFileUtil.FilterFilesWithNameOrContentRegexs(allDataFiles, dicFilterFileRules["TrackFileFilterRules"], null);
                
                if (allfiles.trackFiles.Count == 0)
                    throw new Exception("获取磁条数据文件失败！");

                trackDataCount = allCores.trackFileCore.GetTrackAllCounts(allfiles.trackFiles);
                commentMessage += string.Format(" 磁条:{0}", trackDataCount);
                dataCounts.Add("track", trackDataCount);
            }
            //获取医保数据文件
            if (allCores.medicalFileCore != null)
            {
                allfiles.medicalFiles = new List<string>();
                
                allfiles.medicalFiles = getFileUtil.FilterFilesWithNameOrContentRegexs(allDataFiles, dicFilterFileRules["MedicalFileFilterRules"], null);
                if (allfiles.medicalFiles.Count == 0)
                    throw new Exception("获取医保数据文件失败！");

                medicalDataCount = allCores.medicalFileCore.GetMedicalAllCounts(allfiles.medicalFiles);
                commentMessage += string.Format(" 医保:{0}", medicalDataCount);
                dataCounts.Add("medical", medicalDataCount);
            }
            //获取匹配文件
            if (allCores.matchFileCore!= null)
            {
                allfiles.matchFiles = new List<string>();
                
                allfiles.matchFiles = getFileUtil.FilterFilesWithNameOrContentRegexs(allDataFiles, dicFilterFileRules["MatchFileFilterRules"], null);
                if (allfiles.matchFiles.Count == 0)
                    throw new Exception("获取匹配数据文件失败！");
                matchDataCount = allCores.matchFileCore.GetMatchAllCounts(allfiles.matchFiles);
                commentMessage += string.Format(" 匹配:{0}", matchDataCount);
                dataCounts.Add("match", matchDataCount);
            }

            targetCounts=ssseDataCount != 0 ? ssseDataCount : pbocDataCount;
            return targetCounts;
        }
        /// <summary>
        /// 检查数量
        /// </summary>
        /// <param name="dataCounts">各种数据条数集合</param>
        /// <returns></returns>
        public bool CountCheck(Dictionary<string, int> dataCounts)
        {
            CountCheckConfig config = CountCheckConfig.GetConfig();
  
            if (config!=null &&!config.differIsContinue && config.countCheck != null)
            {
                string[] countCheckRule = config.countCheck;
                string[] pstm = new string[] { "ssse", "pboc", "track", "medical" };
                //判断数据量是否一致
                #region
                for (int i = 0; i < countCheckRule.Length; i++)
                {
                    string[] parameters = countCheckRule[i].Split('^');
                    string match1 = parameters[0].Trim();//第一项
                    string match2 = parameters[1].Trim();//第二项

                    if (pstm.Contains(match1) && pstm.Contains(match2))
                    {
                        int match1Count = int.Parse(dataCounts[match1].ToString());
                        int match2Count = int.Parse(dataCounts[match2].ToString());

                        string errorMessage = string.Format(parameters[2].Trim(), match1Count, match2Count);//提示

                        if (config.differIsContinue && (match1Count == -1 || match2Count == -1))
                            continue;
                        if (match1Count != match2Count)
                        {
                            throw new Exception(errorMessage);
                        }
                    }
                }
                #endregion

                return true;
            }
            return false;
        }
    }
    /// <summary>
    /// 所有核心处理类
    /// </summary>
    public class AllCores
    {
        public PbocFileCore pbocFileCore { set; get; }//金融文件处理核心
        public TrackFileCore trackFileCore { set; get; }//磁条文件处理核心
        public SsseFileCore ssseFileCore { set; get; }//社保文件处理核心
        public MedicalFileCore medicalFileCore { set; get; }//医保文件处理核心
        public MatchFileCore matchFileCore { set; get; }//匹配文件处理核心
        public MatchCore matchCore { set; get; }//匹配处理核心
    }

    public class AllFactoryNames
    {
        /// <summary>
        /// 金融文件
        /// </summary>
        public string pbocFileFactory { set; get; }
        /// <summary>
        /// 磁条文件
        /// </summary>
        public string trackFileFactory { set; get; }
        /// <summary>
        /// 社保文件
        /// </summary>
        public string ssseFileFactory { set; get; }
        /// <summary>
        /// 医保文件
        /// </summary>
        public string medicalFileFactory { set; get; }
        /// <summary>
        /// 匹配文件
        /// </summary>
        public string matchFileFactory { set; get; }
        /// <summary>
        /// 匹配
        /// </summary>
        public string matchFactory { set; get; }   
    }

    public class AllFiles
    {
        /// <summary>
        /// 金融文件
        /// </summary>
       public List<string> pbocFiles { set; get; }
        /// <summary>
        /// 磁条文件
        /// </summary>
       public List<string> trackFiles { set; get; }
        /// <summary>
        /// 社保文件
        /// </summary>
       public List<string> ssseFiles { set; get; }
        /// <summary>
        /// 医保文件
        /// </summary>
       public List<string> medicalFiles { set; get; }
        /// <summary>
        /// 匹配文件
        /// </summary>
       public List<string> matchFiles { set; get; }
    }
}
